<?php
defined('IN_PHPCMS') or exit('No permission resources.');

class tasks {
	private $db;
	public function __construct() {
		$this->db = pc_base::load_model('module_model');
		pc_base::load_app_func('global');
	}
	
	public function init() {
		$siteid = get_siteid();
		if(!$siteid) $siteid = 1;
		
		//加载基本信息开始
		$info = $this->db->get_one(array('module'=>'admin'));
		extract(string2array($info['setting']));
		
		$nodeid = $tasks_collection.'';
		$programid = $tasks_program.'';
		$cus_page = $tasks_number.'';
		
		if(!isset($nodeid) || $nodeid=='') exit;
		if(!isset($programid) || $programid=='') exit;
		if(!isset($cus_page) || $cus_page=='') exit;
		//加载基本信息结束

		$collection_content_db = pc_base::load_model('collection_content_model');
		$program_db = pc_base::load_model('collection_program_model');
		$content_db = pc_base::load_model('content_model');
		
		$programid_array = explode(',',$programid);

		foreach ($programid_array as $programid)
		{
			//查询需要导入信息开始
			$sql = array('nodeid'=>$nodeid, 'siteid'=>$siteid, 'status' => 1);
			
			$data1 = $collection_content_db->select($sql, '*', $cus_page, 'id asc');
			$ids = '';
			foreach ($data1 as $d)
			{
				$ids[] = $d['id'];
			}
			$type = '';
			$attach_status = false;
			if(pc_base::load_config('system','attachment_stat')) {
				$attachment_db = pc_base::load_model('attachment_model');
				$att_index_db = pc_base::load_model('attachment_index_model');
				$attach_status = true;
			}
			$order = 'id desc';
			$ids = implode('\',\'', $ids);
			
			$data = $collection_content_db->select("siteid='".$siteid."' AND id in ('$ids') AND nodeid = '$nodeid' AND status = '1'", 'id, data', '', $order);
			//查询需要导入信息结束
			
	
			$program = $program_db->get_one(array('id'=>$programid));
			$program['config'] = string2array($program['config']);
			$content_db->set_model($program['modelid']);
			$coll_contentid = array();
			
			//加载所有的处理函数
			$funcs_file_list = glob(dirname(__FILE__).DIRECTORY_SEPARATOR.'spider_funs'.DIRECTORY_SEPARATOR.'*.php');
			foreach ($funcs_file_list as $v) {
				include $v;
			}
			$t_catid = $program['catid'];
			
			//采集导入实际处理开始
			foreach ($data as $k=>$v) {
				$sql = array('catid'=>$program['catid'], 'status'=>$program['config']['content_status']);
				$v['data'] = string2array($v['data']);
				
				foreach ($program['config']['map'] as $a=>$b) {
					if (isset($program['config']['funcs'][$a]) && function_exists($program['config']['funcs'][$a])) {
						$GLOBALS['field'] = $a;
						$sql[$a] = $program['config']['funcs'][$a]($v['data'][$b]);
					} else {
						$sql[$a] = $v['data'][$b];
					}
				}
				if ($node['content_page'] == 1) $sql['paginationtype'] = 2;
				
				$sql['username'] = $tasks_username;
				$contentid = $content_db->add_content($sql, 1);
				$contentids = '';
				$coll_contentid = '';
				if ($contentid) {
					$contentids[] = $contentid;
					$coll_contentid[] = $v['id'];
					$i++;
					//更新附件状态,将采集关联重置到内容关联
					if($attach_status) {
						$datas = $att_index_db->select(array('keyid'=>'cj-'.$v['id']),'*',100,'','','aid');
						if(!empty($datas)) {
							$datas = array_keys($datas);
							$datas = implode(',',$datas);
							$att_index_db->update(array('keyid'=>'c-'.$program['catid'].'-'.$contentid),array('keyid'=>'cj-'.$v['id']));
							$attachment_db->update(array('module'=>'content')," aid IN ($datas)");
						}
					}
				} else {
					$collection_content_db->delete(array('id'=>$v['id']));
				}
			}
			$sql_id = implode('\',\'', $coll_contentid);
			$contentids = implode('\',\'', $contentids);
			$collection_content_db->update(array('status'=>2), " id IN ('$sql_id')");
			//采集导入实际处理结束
			
			$html = pc_base::load_app_class('html', 'content');
			
			//更新栏目页开始
			$category = getcache('category_content_'.$siteid,'commons');
			$modelid = $category[$t_catid][modelid];
			$page = 1;
			$j = 1;
			do
			{
				echo $ishtml;
				$html->category($t_catid,$page);
				$page++;
				$j++;
				$total_number = PAGES;
			}
			while ($j <= $total_number);
			//更新栏目页结束
	
			//生成内容页开始
			$content_db->set_model($modelid);
			$table_name = $content_db->table_name;
			$table_name_data = $table_name.'_data';
			$where = " WHERE a.`id`=b.`id` AND a.status=99  AND a.`id` IN('$contentids') ";
			$rs = $content_db->query("SELECT a.*, b.content FROM `$table_name` AS a, `$table_name_data` AS b $where");
			$data = $content_db->fetch_array($rs);
			$tablename = $content_db->table_name.'_data';
			$url = pc_base::load_app_class('url', 'content');
			
			foreach($data as $r) {
				if($r['islink']) continue;
				$table_name = $tablename;
				$r2 = $content_db->get_one(array('id'=>$r['id']));
				if($r) $r = array_merge($r,$r2);
				if($r['upgrade']) {
					$urls[1] = $r['url'];
				} else {
					$urls = $url->show($r['id'], '', $r['catid'],$r['inputtime']);
				}
				$html->show($urls[1],$r,0,'edit',$r['upgrade']);
			}
		}
		//更新首页开始
		$html->index();
		//更新首目页结束
	}
}
